
package model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Set;

/**
 * The Trail Maintenance Jobs Collection Class.
 * @author: Kyle Richardson
 */
public class Jobs implements Serializable{
	/**
	 * The classes serializable ID.
	 */
	private static final long serialVersionUID =
		2050475746117502305L;
	/**
	 * HashMap containing all requested jobs.
	 */
	private HashMap<String, Job> myRequests;
	/**
	 * HashMap containing all available jobs.
	 */
	private HashMap<String, Job> myJobs;
	
	/**
	 * Jobs collection constructor.
	 */
	public Jobs(){
		myRequests = new HashMap<String, Job>();
		myJobs = new HashMap<String, Job>();
	}
	
	/**
	 * Adds a job theJob to the available jobs HashMap.
	 * @param theJob the specified job to be added.
	 * @return Returns true if successful, otherwise false.
	 */
	public boolean addNewJob(final Job theJob)
	{
		boolean result;
		if (!myJobs.containsKey(theJob.getName())) 
		{
			myJobs.put(theJob.getName(), theJob);
			result = true;
		}
		else
		{
			result = false;
		}
		return result;
	}
	
	/**
	 * Adds a requested job theJob to the requested
	 * jobs HashMap
	 * @param theJob the requested job.
	 * @return Returns true if successful, otherwise false.
	 */
	public boolean addNewRequest(final Job theJob)
	{
		boolean result;
		if (!myRequests.containsKey(theJob.getName())) 
		{
			myRequests.put(theJob.getName(), theJob);
			result = true;
		}
		else
		{
			result = false;
		}
		return result;
	}
	
	/**
	 * Deletes a job theJob from the available jobs HashMap
	 * @param theJob The specified job to be deleted.
	 * @return Returns true if successful, otherwise false.
	 */
	public boolean deleteJob(final Job theJob)
	{
		myJobs.remove(theJob.getName());
		return true;
	}
	
	/**
	 * Returns the HashMap of all the Jobs.
	 * @return the HashMap of all Jobs.
	 */
	public HashMap<String, Job> getAllJobs()
	{
		return myJobs;
	}
	
	/**
	 * Returns the HashMap of all requested jobs.
	 * @return the HashMap of all requested jobs.
	 */
	public HashMap<String, Job> getRequestedJobs()
	{
		return myRequests;
	}
	
	/**
	 * not required for official requirements.
	 * @param theLMA
	 * @return
	 */
	public HashMap<String, Job> getLMAJobs(final LMA theLMA)
	{
		//not needed for official requirements.
		return null;
	}
	
	/**
	 * Finds and returns all the Jobs a Volunteer has 
	 * signed up for.
	 * but is not used since it is not part of the offical
	 * requirements.
	 * @param theVolunteer the Volunteer.
	 * @return the HashMap of all the jobs.
	 */
	public HashMap<String, Job> getVolunteersJobs(
			final Volunteer theVolunteer)
	{
		HashMap<String, Job> VolJobs = new HashMap<String,
		Job>();
	
		Set<String> Set = myJobs.keySet();
		for(String n : Set )
		{
			
			if (myJobs.get(n).isSignedUpFor(theVolunteer)){
				VolJobs.put(n, myJobs.get(n));
			}
		}
		return VolJobs;
	}
	
	/**
	 * Finds the specified job and returns it.
	 * @param theJobName the job being looked for.
	 * @return Returns the job when found.
	 */
	public Job findJob(String theJobName)
	{
		//check hashmap is not empty
		Job job = myJobs.get(theJobName);
		return job;
	}
	
	/**
	 * Finds the specified job and returns it.
	 * @param theJobName the job being looked for.
	 * @return Returns the job when found.
	 */
	public Job getRequestedJob(String theJobName)
	{
		//check hashmap is not empty
		Job requestedJob = myRequests.get(theJobName);
		myRequests.remove(theJobName);
		return requestedJob;
	}
	
	/**
	 * not needed for official requirements.
	 * @return
	 */
	public String getSchedulerInfo() {
		//not needed for official requirements.
		return null;
	}

	/**
	 * not needed for official requirements.
	 * @param theRequest
	 */
	public void denyRequest(Job theRequest) {
		//not needed for official requirements.
		
	}
	
	/**
	 * Schedules a job by settings its information.
	 * @param theJob The job being scheduled.
	 * @param theStartYear The starting year.
	 * @param theStartMonth The starting month.
	 * @param theStartDate The starting day.
	 * @param theEndYear The ending year.
	 * @param theEndMonth The ending month.
	 * @param theEndDate The ending day.
	 */
	public void scheduleJob(Job theJob, 
			final int theStartYear,
			final int theStartMonth,
			final int theStartDate, final int theEndYear,
			final int theEndMonth, final int theEndDate)
	{
		myRequests.remove(theJob);
		theJob.schduleJob(theStartYear, theStartMonth, 
				theStartDate, theEndYear, theEndMonth, 
				theEndDate);
		addNewJob(theJob);
	}
}
